<template>
  <!-- 工单管理表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工序id" prop="processId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processId"
              placeholder="请输入工序id"
              clearable
            />

            <span v-else>{{ formData.processId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序编码" prop="processCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processCode"
              placeholder="请输入工序编码"
              clearable
            />

            <span v-else>{{ formData.processCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序名称" prop="processName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.processName"
              placeholder="请输入工序名称"
              clearable
            />

            <span v-else>{{ formData.processName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备id" prop="equipId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipId"
              placeholder="请输入设备id"
              clearable
            />

            <span v-else>{{ formData.equipId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编码" prop="equipCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipCode"
              placeholder="请输入设备编码"
              clearable
            />

            <span v-else>{{ formData.equipCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="equipName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.equipName"
              placeholder="请输入设备名称"
              clearable
            />

            <span v-else>{{ formData.equipName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目id" prop="projectId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.projectId"
              placeholder="请输入项目id"
              clearable
            />

            <span v-else>{{ formData.projectId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目编码" prop="projectCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.projectCode"
              placeholder="请输入项目编码"
              clearable
            />

            <span v-else>{{ formData.projectCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.projectName"
              placeholder="请输入项目名称"
              clearable
            />

            <span v-else>{{ formData.projectName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品型号id" prop="modelId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.modelId"
              placeholder="请输入产品型号id"
              clearable
            />

            <span v-else>{{ formData.modelId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品型号编码" prop="modelCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.modelCode"
              placeholder="请输入产品型号编码"
              clearable
            />

            <span v-else>{{ formData.modelCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品型号名称" prop="modelName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.modelName"
              placeholder="请输入产品型号名称"
              clearable
            />

            <span v-else>{{ formData.modelName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备id" prop="gageEquipId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipId"
              placeholder="请输入检具设备id"
              clearable
            />

            <span v-else>{{ formData.gageEquipId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备编码" prop="gageEquipCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipCode"
              placeholder="请输入检具设备编码"
              clearable
            />

            <span v-else>{{ formData.gageEquipCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备名称" prop="gageEquipName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipName"
              placeholder="请输入检具设备名称"
              clearable
            />

            <span v-else>{{ formData.gageEquipName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备号id" prop="gageEquipMarkId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipMarkId"
              placeholder="请输入检具设备号id"
              clearable
            />

            <span v-else>{{ formData.gageEquipMarkId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备号编码" prop="gageEquipMarkCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipMarkCode"
              placeholder="请输入检具设备号编码"
              clearable
            />

            <span v-else>{{ formData.gageEquipMarkCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检具设备号名称" prop="gageEquipMarkName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.gageEquipMarkName"
              placeholder="请输入检具设备号名称"
              clearable
            />

            <span v-else>{{ formData.gageEquipMarkName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特性项id" prop="paramId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.paramId"
              placeholder="请输入特性项id"
              clearable
            />

            <span v-else>{{ formData.paramId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特性项编码" prop="paramCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.paramCode"
              placeholder="请输入特性项编码"
              clearable
            />

            <span v-else>{{ formData.paramCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="特性项名称" prop="paramName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.paramName"
              placeholder="请输入特性项名称"
              clearable
            />

            <span v-else>{{ formData.paramName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="过程能力类型" prop="abilityType">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.abilityType"
              placeholder="请输入过程能力类型"
              clearable
            />

            <span v-else>{{ formData.abilityType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="问题描述" prop="problemDescription">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.problemDescription"
              placeholder="请输入问题描述"
              clearable
            />

            <span v-else>{{ formData.problemDescription }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原因分析" prop="causeAnalysis">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.causeAnalysis"
              placeholder="请输入原因分析"
              clearable
            />

            <span v-else>{{ formData.causeAnalysis }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="原因分析完成时间"
            prop="causeAnalysisCompletionTime"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.causeAnalysisCompletionTime"
              placeholder="请输入原因分析完成时间"
              clearable
            />

            <span v-else>{{ formData.causeAnalysisCompletionTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原因分析输入时间" prop="causeAnalysisInputTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.causeAnalysisInputTime"
              placeholder="请输入原因分析输入时间"
              clearable
            />

            <span v-else>{{ formData.causeAnalysisInputTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纠正/预防措施" prop="preventiveMeasure">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.preventiveMeasure"
              placeholder="请输入纠正/预防措施"
              clearable
            />

            <span v-else>{{ formData.preventiveMeasure }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="纠正/预防措施完成时间"
            prop="preventiveMeasureCompletionTime"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.preventiveMeasureCompletionTime"
              placeholder="请输入纠正/预防措施完成时间"
              clearable
            />

            <span v-else>{{ formData.preventiveMeasureCompletionTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="纠正/预防措施输入时间"
            prop="preventiveMeasureInputTime"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.preventiveMeasureInputTime"
              placeholder="请输入纠正/预防措施输入时间"
              clearable
            />

            <span v-else>{{ formData.preventiveMeasureInputTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标准化及其它类似问题的纠正/预防措施"
            prop="similarIssues"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.similarIssues"
              placeholder="请输入标准化及其它类似问题的纠正/预防措施"
              clearable
            />

            <span v-else>{{ formData.similarIssues }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标准化及其它类似问题的纠正/预防措施完成时间："
            prop="similarIssuesCompletionTime"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.similarIssuesCompletionTime"
              placeholder="请输入标准化及其它类似问题的纠正/预防措施完成时间："
              clearable
            />

            <span v-else>{{ formData.similarIssuesCompletionTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标准化及其它类似问题的纠正/预防措施输入时间"
            prop="similarIssuesInputTime"
          >
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.similarIssuesInputTime"
              placeholder="请输入标准化及其它类似问题的纠正/预防措施输入时间"
              clearable
            />

            <span v-else>{{ formData.similarIssuesInputTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验证" prop="verify">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.verify"
              placeholder="请输入验证"
              clearable
            />

            <span v-else>{{ formData.verify }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验证完成时间" prop="verifyCompletionTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.verifyCompletionTime"
              placeholder="请输入验证完成时间"
              clearable
            />

            <span v-else>{{ formData.verifyCompletionTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验证输入时间" prop="verifyInputTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.verifyInputTime"
              placeholder="请输入验证输入时间"
              clearable
            />

            <span v-else>{{ formData.verifyInputTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="workOrderState">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workOrderState"
              placeholder="请输入状态"
              clearable
            />

            <span v-else>{{ formData.workOrderState }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据版本号" prop="version">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.version"
              placeholder="请输入数据版本号"
              clearable
            />

            <span v-else>{{ formData.version }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.enabled"
              placeholder="请输入是否启用"
              clearable
            />

            <span v-else>{{ formData.enabled }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="删除标识" prop="deleted">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deleted"
              placeholder="请输入删除标识"
              clearable
            />

            <span v-else>{{ formData.deleted }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />

            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人主键" prop="createdUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserId"
              placeholder="请输入创建人主键"
              clearable
            />

            <span v-else>{{ formData.createdUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人姓名" prop="createdUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              placeholder="请输入创建人姓名"
              clearable
            />

            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人主键" prop="lastUpdatedUserId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserId"
              placeholder="请输入更新人主键"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人姓名" prop="lastUpdatedUserName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdatedUserName"
              placeholder="请输入更新人姓名"
              clearable
            />

            <span v-else>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最后更新时间" prop="lastUpdateTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lastUpdateTime"
              placeholder="请输入最后更新时间"
              clearable
            />

            <span v-else>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPCMMONITORWORKORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  processId: [],
  processCode: [],
  processName: [],
  equipId: [],
  equipCode: [],
  equipName: [],
  projectId: [],
  projectCode: [],
  projectName: [],
  modelId: [],
  modelCode: [],
  modelName: [],
  gageEquipId: [],
  gageEquipCode: [],
  gageEquipName: [],
  gageEquipMarkId: [],
  gageEquipMarkCode: [],
  gageEquipMarkName: [],
  paramId: [],
  paramCode: [],
  paramName: [],
  abilityType: [],
  problemDescription: [],
  causeAnalysis: [],
  causeAnalysisCompletionTime: [],
  causeAnalysisInputTime: [],
  preventiveMeasure: [],
  preventiveMeasureCompletionTime: [],
  preventiveMeasureInputTime: [],
  similarIssues: [],
  similarIssuesCompletionTime: [],
  similarIssuesInputTime: [],
  verify: [],
  verifyCompletionTime: [],
  verifyInputTime: [],
  workOrderState: [],
  version: [],
  enabled: [],
  deleted: [],
  createTime: [],
  createdUserId: [],
  createdUserName: [],
  lastUpdatedUserId: [],
  lastUpdatedUserName: [],
  lastUpdateTime: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  processId: "",
  processCode: "",
  processName: "",
  equipId: "",
  equipCode: "",
  equipName: "",
  projectId: "",
  projectCode: "",
  projectName: "",
  modelId: "",
  modelCode: "",
  modelName: "",
  gageEquipId: "",
  gageEquipCode: "",
  gageEquipName: "",
  gageEquipMarkId: "",
  gageEquipMarkCode: "",
  gageEquipMarkName: "",
  paramId: "",
  paramCode: "",
  paramName: "",
  abilityType: "",
  problemDescription: "",
  causeAnalysis: "",
  causeAnalysisCompletionTime: "",
  causeAnalysisInputTime: "",
  preventiveMeasure: "",
  preventiveMeasureCompletionTime: "",
  preventiveMeasureInputTime: "",
  similarIssues: "",
  similarIssuesCompletionTime: "",
  similarIssuesInputTime: "",
  verify: "",
  verifyCompletionTime: "",
  verifyInputTime: "",
  workOrderState: "",
  version: "",
  enabled: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        processId,
        processCode,
        processName,
        equipId,
        equipCode,
        equipName,
        projectId,
        projectCode,
        projectName,
        modelId,
        modelCode,
        modelName,
        gageEquipId,
        gageEquipCode,
        gageEquipName,
        gageEquipMarkId,
        gageEquipMarkCode,
        gageEquipMarkName,
        paramId,
        paramCode,
        paramName,
        abilityType,
        problemDescription,
        causeAnalysis,
        causeAnalysisCompletionTime,
        causeAnalysisInputTime,
        preventiveMeasure,
        preventiveMeasureCompletionTime,
        preventiveMeasureInputTime,
        similarIssues,
        similarIssuesCompletionTime,
        similarIssuesInputTime,
        verify,
        verifyCompletionTime,
        verifyInputTime,
        workOrderState,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      } = newVal;
      Object.assign(formData, {
        id,
        processId,
        processCode,
        processName,
        equipId,
        equipCode,
        equipName,
        projectId,
        projectCode,
        projectName,
        modelId,
        modelCode,
        modelName,
        gageEquipId,
        gageEquipCode,
        gageEquipName,
        gageEquipMarkId,
        gageEquipMarkCode,
        gageEquipMarkName,
        paramId,
        paramCode,
        paramName,
        abilityType,
        problemDescription,
        causeAnalysis,
        causeAnalysisCompletionTime,
        causeAnalysisInputTime,
        preventiveMeasure,
        preventiveMeasureCompletionTime,
        preventiveMeasureInputTime,
        similarIssues,
        similarIssuesCompletionTime,
        similarIssuesInputTime,
        verify,
        verifyCompletionTime,
        verifyInputTime,
        workOrderState,
        version,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.processId = "";
  formData.processCode = "";
  formData.processName = "";
  formData.equipId = "";
  formData.equipCode = "";
  formData.equipName = "";
  formData.projectId = "";
  formData.projectCode = "";
  formData.projectName = "";
  formData.modelId = "";
  formData.modelCode = "";
  formData.modelName = "";
  formData.gageEquipId = "";
  formData.gageEquipCode = "";
  formData.gageEquipName = "";
  formData.gageEquipMarkId = "";
  formData.gageEquipMarkCode = "";
  formData.gageEquipMarkName = "";
  formData.paramId = "";
  formData.paramCode = "";
  formData.paramName = "";
  formData.abilityType = "";
  formData.problemDescription = "";
  formData.causeAnalysis = "";
  formData.causeAnalysisCompletionTime = "";
  formData.causeAnalysisInputTime = "";
  formData.preventiveMeasure = "";
  formData.preventiveMeasureCompletionTime = "";
  formData.preventiveMeasureInputTime = "";
  formData.similarIssues = "";
  formData.similarIssuesCompletionTime = "";
  formData.similarIssuesInputTime = "";
  formData.verify = "";
  formData.verifyCompletionTime = "";
  formData.verifyInputTime = "";
  formData.workOrderState = "";
  formData.version = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
}

const loading = ref(false);

/** 工单管理保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dmpDosPcm/pcmmonitorworkorder/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
